package cn.fl.splrate.controller;

import cmm.mid.core.framework.dto.DataResultDTO;
import cmm.mid.core.framework.page.ListPageVO;
import cmm.web.core.framework.controller.BaseController;
import cn.cmm.base.framework.constant.BaseConstant;
import cn.cmm.base.framework.logger.eum.LogOperEnum;
import cn.cmm.base.framework.utils.BaseLogUtils;
import cn.cmm.base.framework.utils.CollectionUtils;
import cn.cmm.base.framework.utils.StringUtils;
import cn.fl.framework.base.context.CurrentThreadContext;
import cn.fl.splrate.transform.SplRelDataOpRecVOTransform;
import cn.fl.splrate.vo.SplRelDataOpRecVO;
import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import fl.constant.facade.framework.constant.BusinessIdConstant;
import fl.mongo.facade.base.page.PaginationVo;
import fl.mongo.facade.spl.api.ISplRelDataOpRecQueryApiService;
import fl.mongo.facade.spl.condition.SplRelDataOpRecQC;
import fl.mongo.facade.spl.dto.SplRelDataOpRecDTO;
import fl.spl.facade.framework.constant.SplSystemConstant;
import fl.spl.facade.framework.enums.CommonOpTypCdEnum;
import fl.spl.facade.framework.enums.common.MagicNumReplaceEnum;
import fl.spl.facade.framework.enums.splrate.BasicDataRelOpBizTypCdEnum;
import fl.spl.facade.splrate.api.ISplRateRelInfoQueryApiService;
import fl.spl.facade.splrate.condition.SplRateRelInfoQC;
import fl.spl.facade.splrate.dto.SplRateRelInfoDTO;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @Description: 供应商相关数据操作日志记录表 控制层
 */
@Controller
@RequestMapping("/mongo/splRelDataOpRec")
public class SplRelDataOpRecController extends BaseController {

    @Autowired
    private ISplRelDataOpRecQueryApiService splRelDataOpRecQueryApiService;

    @Autowired
    private ISplRateRelInfoQueryApiService splRateRelInfoQueryApiService;

    /**
     * 到列表页面
     */
    @RequestMapping(value = "/toListPage.do", method = {RequestMethod.GET, RequestMethod.POST})
    public ModelAndView toListPage(HttpServletRequest request, HttpServletResponse response) {
        Long startTm = System.currentTimeMillis();
        BaseLogUtils.newWebLogger("加载列表页面，请求参数：" + this.getRequestParameter(request)).setLogOperType(LogOperEnum.REQ)
                .setSysCd(CurrentThreadContext.getCurrentSysCd())
                .setChannelCd(SplSystemConstant.FL_WEB_CHL_CD)
                .setBussinessId(BusinessIdConstant.LOG_SPL_MANAGER_CD)
                .setWarning(false)
                .info();
        ModelAndView view = new ModelAndView("splrate/splRelDataOpRec/splRelDataOpRecList");

        //当前业务主键ID
        String pkId = request.getParameter("pkId");
        //数据对象主键ID
        String dataId = request.getParameter("dataId");
        //数据对象名称
        String dataNm = request.getParameter("dataNm");
        //操作对象类型("SPL":经销商,"LNK":挂靠公司,etc.)
        String opObjTyp = request.getParameter("opObjTyp");
        //操作数据类型("SPLRATE":经销商评级,etc.)
        String dataTypCd = request.getParameter("dataTypCd");
        //操作数据表信息("SPL_BSC_INFO":经销商,"PRJ_LNK_COMP":挂靠公司,"SPL_RATE_REL_INFO":挂靠公司评分评级表)
        String opDataTabNm = request.getParameter("opDataTabNm");

        if (StringUtils.isEmpty(opObjTyp)) {
            opObjTyp = BasicDataRelOpBizTypCdEnum.SPL_SPLRATE.getTableSimpleNm();
        }
        if (StringUtils.isEmpty(dataTypCd)) {
            dataTypCd = BasicDataRelOpBizTypCdEnum.SPL_SPLRATE.getBizTypCd();
        }
        if (StringUtils.isEmpty(opDataTabNm)) {
            opDataTabNm = BasicDataRelOpBizTypCdEnum.SPL_SPLRATE.getTableNm();
        }
        view.addObject("pkId", pkId);
        view.addObject("dataId", dataId);
        view.addObject("dataNm", dataNm);
        view.addObject("opObjTyp", opObjTyp);
        view.addObject("dataTypCd", dataTypCd);

        BaseLogUtils.newWebLogger("返回数据view:" + JSON.toJSONString(view) + "加载列表页面").setLogOperType(LogOperEnum.REQ)
                .setSysCd(CurrentThreadContext.getCurrentSysCd())
                .setChannelCd(SplSystemConstant.FL_WEB_CHL_CD)
                .setBussinessId(BusinessIdConstant.LOG_SPL_MANAGER_CD)
                .setWarning(false)
                .info();
        return view;
    }

    /**
     * 初始化列表
     */
    @SuppressWarnings("unchecked")
    @RequestMapping(value = "/initList.do", method = {RequestMethod.GET, RequestMethod.POST})
    @ResponseBody
    public Object initList(HttpServletRequest request, HttpServletResponse response) {
        BaseLogUtils.newWebLogger("加载列表页面，请求参数：" + this.getRequestParameter(request)).setLogOperType(LogOperEnum.REQ)
                .setSysCd(CurrentThreadContext.getCurrentSysCd())
                .setChannelCd(SplSystemConstant.FL_WEB_CHL_CD)
                .setBussinessId(BusinessIdConstant.LOG_SPL_MANAGER_CD)
                .setWarning(false)
                .info();
        ListPageVO<SplRelDataOpRecVO> listPage = null;

        String currentPage = request.getParameter("page");
        String pageSize = request.getParameter("pagesize");
        //当前业务主键ID
        String pkId = request.getParameter("pkId");
        //数据对象主键ID
        String dataId = request.getParameter("dataId");
        //数据对象名称
        String dataNm = request.getParameter("dataNm");
        //操作对象类型("SPL":经销商,"LNK":挂靠公司,etc.)
        String opObjTyp = request.getParameter("opObjTyp");
        //操作数据类型("SPLRATE":经销商评级,etc.)
        String dataTypCd = request.getParameter("dataTypCd");

        if (StringUtils.isEmpty(dataId)) {
            listPage = new ListPageVO<SplRelDataOpRecVO>(new ArrayList<SplRelDataOpRecVO>(), 0);
            return listPage;
        }
        PaginationVo paginationVo = new PaginationVo();
        if (StringUtils.isNotEmpty(currentPage)) {
            paginationVo.setPageNo(Integer.parseInt(currentPage));
        } else {
            paginationVo.setPageNo(BaseConstant.IS_YESNO_YES);
        }

        if (StringUtils.isNotEmpty(pageSize)) {
            paginationVo.setPageSize(Integer.parseInt(pageSize));
        } else {
            paginationVo.setPageSize(10);
        }

        SplRelDataOpRecQC condition = new SplRelDataOpRecQC();
        condition.setDataId(Long.valueOf(dataId));
        condition.setIsDel(BaseConstant.IS_YESNO_NO);
        condition.setIsValid(BaseConstant.IS_YESNO_YES);
        if (StringUtils.isNotEmpty(opObjTyp)) {
            condition.setOpObjTyp(opObjTyp);
        }
        if (StringUtils.isNotEmpty(pkId)) {
            condition.setPkId(Long.valueOf(pkId));
        }
        if (StringUtils.isNotEmpty(dataNm)) {
            condition.setDataNm(dataNm);
        }
        if (StringUtils.isNotEmpty(dataTypCd)) {
            condition.setDataTypCd(dataTypCd);
        }
        paginationVo.setCondition(condition);
        PaginationVo searchListPage = this.splRelDataOpRecQueryApiService.findSplRelDataOpRecDTOByPage(paginationVo);
        if (null == searchListPage) {
            listPage = new ListPageVO<SplRelDataOpRecVO>(new ArrayList<SplRelDataOpRecVO>(), 0);
            return listPage;
        }

        List<SplRelDataOpRecDTO> splRelDataOpRecDTOS = (List<SplRelDataOpRecDTO>) searchListPage.getDatas();

        if (CollectionUtils.isEmpty(splRelDataOpRecDTOS)) {
            listPage = new ListPageVO<SplRelDataOpRecVO>(new ArrayList<SplRelDataOpRecVO>(), 0);
            return listPage;
        }

        List<Long> splRateIdList = new ArrayList<>(splRelDataOpRecDTOS.size());
        for (SplRelDataOpRecDTO splRelDataOpRecDTO : splRelDataOpRecDTOS) {
            splRateIdList.add(splRelDataOpRecDTO.getPkId());
        }
        Map<Long, String> splRateIdAndVerNmMap = null;
        if (CollectionUtils.isNotEmpty(splRateIdList)) {
            SplRateRelInfoQC splRateRelInfoQC = new SplRateRelInfoQC();
            splRateRelInfoQC.setIdList(splRateIdList);
            splRateRelInfoQC.setIsDel(BaseConstant.IS_YESNO_NO);
            splRateRelInfoQC.setIsSuccess(BaseConstant.IS_YESNO_YES);
            splRateRelInfoQC.setIsNew(MagicNumReplaceEnum.REP_IGNORE_VAL.getVal());
            splRateRelInfoQC.setIsValid(BaseConstant.IS_YESNO_YES);
            DataResultDTO dataResultDTO = this.splRateRelInfoQueryApiService.searchSplRateRelInfoDTOList(splRateRelInfoQC, SplSystemConstant.FL_WEB_CHL_CD, SplSystemConstant.FL_SYS_CD, "");
            List<SplRateRelInfoDTO> splRateRelInfoDTOList = null;
            if (dataResultDTO != null && dataResultDTO.getSuccess() == BaseConstant.IS_YESNO_YES) {
                splRateRelInfoDTOList = (List<SplRateRelInfoDTO>) dataResultDTO.getData();
            }
            if (CollectionUtils.isNotEmpty(splRateRelInfoDTOList)) {
                splRateIdAndVerNmMap = new HashMap<>(splRateRelInfoDTOList.size());
                for (SplRateRelInfoDTO splRateRelInfoDTO : splRateRelInfoDTOList) {
                    splRateIdAndVerNmMap.put(splRateRelInfoDTO.getId(), splRateRelInfoDTO.getVerNum());
                }
            }
        }
        List<SplRelDataOpRecVO> splRelDataOpRecVOList = new ArrayList<SplRelDataOpRecVO>(splRelDataOpRecDTOS.size());
        if (null != splRateIdAndVerNmMap) {
            SplRelDataOpRecVO splRelDataOpRecVO = null;
            for (SplRelDataOpRecDTO splRelDataOpRecDTO : splRelDataOpRecDTOS) {
                splRelDataOpRecVO = SplRelDataOpRecVOTransform.toPO(splRelDataOpRecDTO);
                splRelDataOpRecVO.setVerNum(splRateIdAndVerNmMap.get(splRelDataOpRecDTO.getPkId()));
                splRelDataOpRecVO.setOpTypCdNm(CommonOpTypCdEnum.getOpTypCdNmByOpTypCd(splRelDataOpRecVO.getOpTypCd()));
                splRelDataOpRecVOList.add(splRelDataOpRecVO);
            }
        }
        listPage = new ListPageVO<SplRelDataOpRecVO>(splRelDataOpRecVOList, Integer.valueOf(String.valueOf(searchListPage.getTotalCount())));
        BaseLogUtils.newWebLogger("加载列表页面").setLogOperType(LogOperEnum.REQ)
                .setSysCd(CurrentThreadContext.getCurrentSysCd())
                .setChannelCd(SplSystemConstant.FL_WEB_CHL_CD)
                .setBussinessId(BusinessIdConstant.LOG_SPL_MANAGER_CD)
                .setWarning(false)
                .setKeys("listPage", JSONObject.toJSONString(listPage))
                .info();
        return listPage;
    }
}

